//
//  Formatters.swift
//  swiftProductTemplate
//
//  Created by 刘圣洁 on 2025/6/19.
//

import Foundation

// 格式化工具类
class Formatters {
    // 日期格式化
    static let shared = Formatters()
    
    let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    
    let timeFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm:ss"
        return formatter
    }()
    
    // 货币格式化
    func formatCurrency(_ amount: Double) -> String {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        formatter.locale = Locale.current
        return formatter.string(from: NSNumber(value: amount)) ?? ""
    }
    
    // 手机号格式化
    func formatPhoneNumber(_ phone: String) -> String {
        // 格式化为 XXX-XXXX-XXXX
        let cleaned = phone.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
        let mask = "XXX-XXXX-XXXX"
        
        var result = ""
        var index = cleaned.startIndex
        
        for character in mask where index < cleaned.endIndex {
            if character == "X" {
                result.append(cleaned[index])
                index = cleaned.index(after: index)
            } else {
                result.append(character)
            }
        }
        return result
    }
}    
